<?php
namespace App\Http\Controllers\Admin;
use App\http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdminController extends Controller{
    public function index(){
        return view('admin/index');
        //视图文件是 resources/views/admin/index.blade.php
    }
    public function toget_admin_count(Request $request){
        $count = DB::table('admin');
        if($request->admin_name){
            $count = $count->where('admin_name','like','%'.$request->admin_name.'%');
        }
        if($request->start){
            $count = $count->where('admin_addtime','>',strtotime($request->start));
        }
        if($request->end){
            $count = $count->where('admin_addtime','<',strtotime($request->end));
        }
        $count = $count->count();
        echo $count;die;
    }
    public function toget_admin_list(Request $request){
        $admin_p= DB::table('admin')
            ->orderBy("admin_addtime","DESC");
        //echo $request->admin_name;die;
        if($request->admin_name){
            $admin_p = $admin_p->where('admin_name','like','%'.$request->admin_name.'%');
        }
        if($request->start){
            $admin_p = $admin_p->where('admin_addtime','>',strtotime($request->start));
        }
        if($request->end){
            $admin_p = $admin_p->where('admin_addtime','<',strtotime($request->end));
        }
        $admin_p = $admin_p->paginate(5);

        //dd($admin_p);die();
           // ->paginate(5);
       //dd($admin_p);die();

        $admin = $admin_p->items();
        $current = $admin_p->currentPage();
        $a = ($current-1)*5+1;
        echo '
        <table class="layui-table">
            <thead>
                <tr>
                    <th>
                    <input type="checkbox" name="all" value="" />
                    </th>
                    <th>编号</th>
                    <th>管理员名称</th>
                    <th>添加时间</th>
                    <th >操作</th>
                </tr>
            </thead>
            <tbody>
        ';

//        echo '<pre>';
//         print_r($admin);
//        echo '</pre>';

        foreach ($admin as $value){
            $admin_time = date('Y-m-d',$value->admin_addtime);
            //echo $admin_time;
            echo '
            <tr>
                <td>
                <input type="checkbox" name="id[]" value="'.$value->admin_id.'" />
                </td>
                <td>'.$a++.'</td>
                <td>'.$value->admin_name.'</td>
                <td>'.$admin_time.'</td>
                <td class="td-manage">
                    <a title="查看"  onclick="x_admin_show('."'编辑'".','."'/admin/admin_edit/$value->admin_id'".')" href="javascript:;">
                        <i class="layui-icon">&#xe63c;</i>
                    </a>
                    <a title="删除" onclick="member_del(this,'.$value->admin_id.')" href="javascript:;">
                        <i class="layui-icon">&#xe640;</i>
                    </a>
                </td>
            </tr>
            ';

        }
        echo '
            </tbody>
            </table>
            <div class="page">'.$admin_p->render().'
            </div>
        ';
        // echo 13213;
        //show_msg('3213','/');
    }
    public function admin_list(){

        return view('admin/admin/admin_list');
    }
    public function admin_add(){

        // echo 13213;
//        show_msg('3213','/');
        return view('admin/admin/admin_add');
    }
    public function todo_addadmin(Request $request){
        $data = array(
            "admin_name"=>$request->admin_name,
            "admin_pwd"=>md5($request->admin_pwd),
            "admin_addtime"=>strtotime($request->admin_addtime)
        );
        if($request->hasFile('file')&&$request->file('file')->isValid()){
            $file=$request->file('file');
            $allowed_extensions = ["png", "jpg", "gif"];
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"只能上传png,jpg和gif格式的图片"
                ));die;
            }else{
                $destinationPath = 'uploads/admin/'; //public 文件夹下面建 uploads 文件夹
                $extension = $file->getClientOriginalExtension();
                $fileName=md5(time().rand(1,1000)).'.'.$extension;
                $file->move($destinationPath,$fileName);
                $data["admin_photo"]="admin/".$fileName;

            }
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"图片上传失败请重试"
            ));die;
        }
        $res = DB::table("admin")->insert($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"添加成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }


    }
    public function admin_edit($admin_id){
        $admin = DB::table('admin')->where('admin_id',$admin_id)->first();
        $admin->admin_addtime = date('Y-m-d',$admin->admin_addtime);
        //dd($admin);

        return view('admin/admin/admin_edit',["admin"=>$admin]);
    }
    public function todo_admin_edit(Request $request){
        $admin_p = DB::table('admin')->select('admin_pwd')->where('admin_id',"$request->admin_id")->first();
        if($request->admin_pwd != ''){
            $pwd = $request->admin_pwd;
        }else{
            $pwd = $admin_p->admin_pwd;
        }

        $data = array(
            "admin_name"=>$request->admin_name,
            "admin_pwd"=>$pwd,
            "admin_addtime"=>strtotime($request->admin_addtime)
        );
        if($request->hasFile('file')&&$request->file('file')->isValid()){
            $file=$request->file('file');
            $allowed_extensions = ["png", "jpg", "gif"];
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"只能上传png,jpg和gif格式的图片"
                ));die;
            }else{
                //删除旧图片
                $old=DB::table('admin')->select('admin_photo')->where("admin_id",$request->admin_id)->first();
                $admin_photo=$old->admin_photo;//文件名
                $path='./uploads/'.$admin_photo;//完整路径
                if(!empty($admin_photo) && file_exists($path)){
                    unlink($path);//当图片字段不为空并且有该图片文件的时候就删除
                }
                $destinationPath = 'uploads/admin/'; //public 文件夹下面建 uploads 文件夹
                $extension = $file->getClientOriginalExtension();
                $fileName=md5(time().rand(1,1000)).'.'.$extension;
                $file->move($destinationPath,$fileName);
                $data["admin_photo"]="admin/".$fileName;

            }
        }
        $res = DB::table("admin")->update($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"编辑成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    public function admin_del(){

    }
    public function todo_admin_del(Request $request){
        $admin_id = $request->admin_id;
        $res = DB::table('admin')->where('admin_id',"$admin_id")->delete();
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    public function todo_admin_delall(Request $request){
        // echo json_encode($request->arr);
        $res = DB::table('admin')->whereIn("admin_id",$request->arr)->delete();
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
}